function start(){
    storeDatabase();
    console.log(database.aboutMe.photo);
    console.log(database.portfolio[0].thumb);
    tmp = user;
    user = tmp.substr(0,1).toUpperCase() + tmp.substr(1);
    tmp = document.getElementById("adminPanel").innerHTML;
    document.getElementById("adminPanel").innerHTML = "<span style='font-size:18px;font-weight:bold;margin-right:70%;color:#777777;'>Painel admin do site. Bem vindo " + user + "!</span><a href='?action=logout' style='color:#777777;'>Sair-></a><br/><br/><br/>" + tmp;
    document.title = "CMS do site | Bem vindo";
    document.getElementById("formLogIn").style.display = "none";
    document.getElementById("adminPanel").style.display = "inherit";
    current = 0;
    
    var quoteLink0 = document.getElementsByClassName("quoteLink");
    quoteLink0 = quoteLink0[0];
    quoteLink0.style.backgroundColor = "#cccccc";
    document.getElementById('quoteAuthor').value = database.homePage[0].author;
    document.getElementById('quote').value = database.homePage[0].quote;
    
    document.getElementById('aboutMePhoto').src = database.aboutMe.photo;
    document.getElementById('aboutMeDesc').value = database.aboutMe.desc;
    for(var i = 0; i < database.aboutMe.skills.length; i++){
        var skillRow = document.createElement('tr');
        var remSkill = document.createElement('td');
        remSkill.innerHTML = '<a title="Remover" href="#delSkill' + (i + 1) + '" name="' + i + '" onClick="removeSkill(event)"><img src="img/delete.png"></a>';
        var skillName = document.createElement('td');
        skillName.innerHTML = '<input id="sn' + i + '" name="skillName" style="width:100%;" type="text" value="' + database.aboutMe.skills[i].name + '" onChange="updateAboutMeData(event)"/>';
        var skillLevel = document.createElement('td');
        skillLevel.innerHTML = '<input id="sl' + i + '" name="skillLevel" style="width:75px;" type="text" maxlength="3" value="' + database.aboutMe.skills[i].level + '" onkeypress="onlyNumbers(event)" onChange="updateAboutMeData(event)"/>';
        
        skillRow.appendChild(remSkill);
        skillRow.appendChild(skillName);
        skillRow.appendChild(skillLevel);
        
        document.getElementById('skillsTable').appendChild(skillRow);
    console.log(database.aboutMe.photo);
    console.log(database.portfolio[0].thumb);
    }
    
    document.getElementById("showCounter").innerHTML = "Mostrando de 1 a " + (database.portfolio.length >= 10 ? "10" : database.portfolio.length) + " de " + database.portfolio.length + ' &iacute;tens.';
    var portfolioList = document.createElement('ul');
    portfolioList.id = "portPage1";
    document.getElementById('portfolioList').appendChild(portfolioList);
    for(i = 0; i < database.portfolio.length; i++){
        if(i / pageCounter == 10){
            pageCounter++;
            portfolioList = document.createElement('ul');
            portfolioList.id = "portPage" + pageCounter;
            portfolioList.style.display = "none";
            document.getElementById('portfolioList').appendChild(portfolioList);
        }
        var portItem = document.createElement('li');
        portItem.className = "portfolioLink";
        portItem.innerHTML = '<a title="Remover" name="' + i + '" href="#delPortfolio' + (i + 1) + '" onClick="removePortfolio(event)"><img style="margin:5px 15px 0px 0px;" src="img/delete.png"></a><a name="' + i +' " href="#portfolio' + (i + 1) + '" onClick="changePortfolio(event)">&Iacute;tem ' + (i + 1) + '</a>';
        portfolioList.appendChild(portItem);
    }
    totalPages = pageCounter;
    pageCounter = 1;
    var item0 = document.getElementsByClassName("portfolioLink");
    item0 = item0[0];
    item0.style.backgroundColor = "#cccccc";
    document.getElementById('portfolioThumb').src = database.portfolio[current].thumb;
    document.getElementById('pageCounter').innerHTML = '<input title="P&aacute;gina anterior" type="button" value="<<" onClick="changePortPage(-1)"/> pág. ' + pageCounter + '/' + totalPages + '<input title="Pr&oacute;xima p&aacute;gina" type="button" value=">>" onClick="changePortPage(1)"/> Ir para a pág. ' + '<input id="goPage" style="width:50px;" type="text" value="" onkeypress="onlyNumbers(event)"/><input type="button" value="Ir" onClick="getPageAndGo()">';
    document.getElementById('portName').value = database.portfolio[current].name;
    document.getElementById('portDesc').value = database.portfolio[current].desc;
    document.getElementById('portTech').value = database.portfolio[current].tech;
    document.getElementById('portURL').value = database.portfolio[current].link;
    var statusOptions = document.getElementsByName('statusOption');
    statusOptions[(database.portfolio[current].status == 1 ? 0 : 1)].selected = 'true';
    showContent(currentTab);
}

function mainLinkClickHandler(e){
    showContent(e.target.name);
}
            
function showContent(tabName){
    current = 0;
    var tab = document.getElementsByName(tabName);
    tab = tab[0];
    var mainLinks = document.getElementsByClassName("mainLink");
    var contents = document.getElementsByClassName("content");
    for(i = 0; i < mainLinks.length; i++){
        mainLinks[i].style.fontWeight = "normal";
        mainLinks[i].style.border = "0";
        mainLinks[i].style.color = "#0000ff";
        contents[i].style.display = "none";
    }
    document.getElementById(tabName).style.display = "inherit";
    tab.style.fontWeight = "bold";
    tab.style.border = "1px solid #000000";
    tab.style.color = "#000000";
    
    if(tabName == 'homePage'){
        switchQuote();
    } else if(tabName == 'portfolio'){
        if(window.portItemChoice != undefined){
            current = portItemChoice;
            portItemChoice = undefined;
        }
        switchItem(current);
        
        if(window.chosenPortPage != undefined){
            chosenPortPage = parseInt((chosenPortPage == 'last' ? totalPages : chosenPortPage));
            changePortPage(chosenPortPage, true);
            chosenPortPage = undefined;
        } else{
            changePortPage(1, true);
        }
    }
}

function overlayLightBox(){
    var lightBox =  document.getElementById('lightBox');
    var lightBoxContent =  document.getElementById('lightBoxContent');
    
    if(lightBox.style.display == 'none'){
        lightBox.style.display = 'inherit';
        lightBoxContent.style.display = 'inherit';
    } else if(lightBox.style.display == 'inherit'){
        lightBox.style.display = 'none';
        lightBoxContent.style.display = 'none';
    }
}

function uploadImage(e){
    var saveConfirm = confirm("Todas as alterações não salvas serão perdidas. Deseja continuar?");
    if(saveConfirm){
        e.target.parentNode.submit();
    } else{
        e.target.value = "";
    }
}

function save(){
    var confirmDialog = confirm("Deseja salvar as alterações de todas as abas?");
    if(confirmDialog){
        var postLength = 0;
        var sendForm = document.createElement('form');
        sendForm.action = '?action=save';
        sendForm.method = 'post';
        sendForm.id = 'sendForm';
        sendForm.style.display = 'none';
            
        var emptyFieldError = false;
        for(var i = 0; i < database.homePage.length; i++){
            if(database.homePage[i].author == "" || database.homePage[i].quote == ""){
                emptyFieldError = true;
                alert("ERRO: Há citações famosas sem autor e/ou texto definidos.");
                break;
            } else{
                postLength++;
                var formChild = document.createElement('input');
                formChild.type = 'text';
                formChild.value = database.homePage[i].author + '§' + database.homePage[i].quote;
                formChild.name = "quote[]";
                sendForm.appendChild(formChild);
            }
        }
        
        for(i = 0; i < database.portfolio.length; i++){
            if(database.portfolio[i].name == "" ||
                database.portfolio[i].desc == "" ||
                database.portfolio[i].tech == "" ||
                database.portfolio[i].link == ""){
                emptyFieldError = true;
                alert("ERRO: Estão faltando informações de alguns ítens do portfolio.");
                break;
            } else{
                postLength++;
                formChild = document.createElement('input');
                formChild.type = 'text';
                formChild.value = database.portfolio[i].thumb + '§' + database.portfolio[i].name + '§' + database.portfolio[i].desc + '§' + database.portfolio[i].tech + '§' + database.portfolio[i].link + '§' + database.portfolio[i].status;
                formChild.name = "portfolio[]";
                sendForm.appendChild(formChild);
            }
        }
        
        if(database.aboutMe.desc == ""){
            emptyFieldError = true;
            alert("Erro: O campo descrição em \"Sobre Mim\" não pode estar vazio.");
        } else{
            postLength++;
            formChild = document.createElement('input');
            formChild.type = 'text';
            formChild.value = database.aboutMe.photo;
            formChild.name = "aboutMe_photo";
            sendForm.appendChild(formChild);
            
            postLength++;
            formChild = document.createElement('input');
            formChild.type = 'text';
            formChild.value = database.aboutMe.desc;
            formChild.name = "aboutMe_desc";
            sendForm.appendChild(formChild);
            
            for(i = 0; i < database.aboutMe.skills.length; i++){
                if(database.aboutMe.skills[i].name == "" || database.aboutMe.skills[i].level == ""){
                    emptyFieldError = true;
                    alert("ERRO: Há skills sem nome e/ou nível definidos.");
                    break;
                } else{
                    postLength++;
                    formChild = document.createElement('input');
                    formChild.type = 'text';
                    formChild.value = database.aboutMe.skills[i].name + "§" + database.aboutMe.skills[i].level;
                    formChild.name = "aboutMe_skill[]";
                    sendForm.appendChild(formChild);
                }
            }
        }
    
        if(!emptyFieldError){
            formChild = document.createElement('input');
            formChild.type = 'hidden';
            formChild.value = postLength;
            formChild.name = 'postLength';
            sendForm.appendChild(formChild);
            var tagBody = document.getElementsByTagName('body');
            tagBody = tagBody[0];
            tagBody.appendChild(sendForm);
            document.getElementById('sendForm').submit();
        }
    }
}

////////HOME PAGE////////
function changeQuote(e){
    current = e.target.name;
    switchQuote();
}

function switchQuote(){
    var author = document.getElementById("quoteAuthor");
    author.value = database.homePage[current].author;
    var quote = document.getElementById("quote");
    quote.value = database.homePage[current].quote;
    var quoteLinks =  document.getElementsByClassName("quoteLink");
    for(var i = 0; i < quoteLinks.length; i++){
        quoteLinks[i].style.backgroundColor = "#ffffff";
    }
    quoteLinks[current].style.backgroundColor = "#cccccc";
}
            
function newQuote(){
    if(database.homePage.length <= 8){
        var obj = new Object();
        obj.author = "";
        obj.quote = "";
        database.homePage.push(obj);
            
        current = database.homePage.length - 1;
        
        var quoteLink = document.createElement('li');
        quoteLink.className = "quoteLink";
        quoteLink.innerHTML = '<a name="' + current + '" href="#famousQuote' + database.homePage.length + '" onClick="changeQuote(event)">Frase ' + database.homePage.length + '</a><a title="Remover" name="' + current + '" href="#delQuote' + database.homePage.length + '" onClick="removeQuote(event)"><img style="margin:5px 0px 0px 15px;" src="img/delete.png"></a>';
        
        document.getElementById('quotesList').appendChild(quoteLink);
        document.getElementById("quoteAuthor").value = "";
        document.getElementById("quote").value = "";
        var quoteLinks =  document.getElementsByClassName("quoteLink");
        for(var i = 0; i < quoteLinks.length; i++){
            quoteLinks[i].style.backgroundColor = "#ffffff";
        }
        quoteLinks[current].style.backgroundColor = "#cccccc";
    } else{
        alert("Você não pode criar mais do que 9 frases.");
    }
}
    
function removeQuote(e){
    var delConfirm = confirm("Deseja remover a Frase " + (parseInt(e.currentTarget.name) + 1) + " escrita por \"" + database.homePage[parseInt(e.currentTarget.name)].author + "\" da lista de citações famosas?");
    
    if(delConfirm){
        if(database.homePage.length > 1){
            if(parseInt(e.currentTarget.name) == database.homePage.length - 1){
                database.homePage.pop();
            } else {
                for(var i = parseInt(e.currentTarget.name); i < database.homePage.length - 1; i++){
                    database.homePage[i] = database.homePage[i + 1];
                }
                database.homePage.pop();
            }
        
            var quotesList = document.getElementById('quotesList');
            quotesList.innerHTML = "";
            for(i = 0; i < database.homePage.length; i++){
                var quoteLink = document.createElement('li');
                quoteLink.className = "quoteLink";
                quoteLink.innerHTML = '<a name="' + i + '" href="#famousQuote' + (i + 1) + '" onClick="changeQuote(event)">Frase ' + (i + 1) + '</a><a title="Remover" name="' + i + '" href="#delQuote' + (i + 1) + '" onClick="removeQuote(event)"><img style="margin:5px 0px 0px 15px;" src="img/delete.png"></a>';
                quotesList.appendChild(quoteLink);
            }
            current = 0;
            var quoteLink0 = document.getElementsByClassName("quoteLink");
            quoteLink0 = quoteLink0[current];
            quoteLink0.style.backgroundColor = "#cccccc";
            document.getElementById('quoteAuthor').value = database.homePage[current].author;
            document.getElementById('quote').value = database.homePage[current].quote;
        } else{
            alert("O número mínimo de frases é 1.");
        }
    }
}
            
function updateQuoteData(e){
    if(e.target.id == "quote"){
        database.homePage[current].quote = e.target.value;
    } else if(e.target.id == "quoteAuthor"){
        database.homePage[current].author = e.target.value;
    }
}
////////HOME PAGE////////

////////PORTFOLIO////////
function changePortfolio(e){
    switchItem(e.target.name);
}
function switchItem(choice){
    current = parseInt(choice);
    var portfolioLinks = document.getElementsByClassName('portfolioLink');
    for(var i = 0; i < portfolioLinks.length; i++){
        portfolioLinks[i].style.backgroundColor = "#ffffff";
    }
    portfolioLinks[current].style.backgroundColor = "#cccccc";
    document.getElementById('portfolioThumb').src = database.portfolio[current].thumb;
    document.getElementById('portName').value = database.portfolio[current].name;
    document.getElementById('portDesc').value = database.portfolio[current].desc;
    document.getElementById('portTech').value = database.portfolio[current].tech;
    document.getElementById('portURL').value = database.portfolio[current].link;
    var statusOptions = document.getElementsByName('statusOption');
    statusOptions[(database.portfolio[current].status == 1 ? 0 : 1)].selected = 'true';
    document.getElementById('thumbIndex').value = current;
}

function getPageAndGo(){
    var goPage = document.getElementById('goPage');
    var getPage = goPage.value;
    if(getPage != ""){
        if(getPage > 0 && getPage <= totalPages){
            changePortPage(getPage, true);
        } else{
            alert('Página "' + getPage + '" inexistente!');
            goPage.value = "";
        }
    }
}

function changePortPage(currentPage, direct){
    direct = (direct !== undefined ? direct : false);
    var conditionParameter = parseInt((direct ? currentPage : (currentPage + pageCounter)));
    
    if(conditionParameter >= 1 && conditionParameter <= totalPages){
        document.getElementById('portPage' + pageCounter).style.display = "none";
        pageCounter = conditionParameter;
        document.getElementById('portPage' + pageCounter).style.display = "inherit";
    }
    document.getElementById("showCounter").innerHTML = "Mostrando de " + (1 + (parseInt(pageCounter) - 1) * 10) + " a " + (database.portfolio.length >= 10 ? (pageCounter == totalPages ? database.portfolio.length : parseInt(pageCounter) * 10) : database.portfolio.length) + " de " + database.portfolio.length + ' &iacute;tens.';
    document.getElementById('pageCounter').innerHTML = '<input title="P&aacute;gina anterior" type="button" value="<<" onClick="changePortPage(-1)"/> pág. ' + pageCounter + '/' + totalPages + '<input title="Pr&oacute;xima p&aacute;gina" type="button" value=">>" onClick="changePortPage(1)"/> Ir para a pág. ' + '<input id="goPage" style="width:50px;" type="text" value="" onkeypress="onlyNumbers(event)"/><input type="button" value="Ir" onClick="getPageAndGo()">';
    document.getElementById('goPage').value = "";
    document.getElementById('portPage').value = pageCounter;
}

function removePortfolio(e){
    var delConfirm = confirm("Deseja remover o ítem " + (parseInt(e.currentTarget.name) + 1) + " do portfolio?");
    
    if(delConfirm){
        if(database.portfolio.length > 1){
            if(parseInt(e.currentTarget.name) == database.portfolio.length - 1){
                database.portfolio.pop();
            } else {
                for(var i = parseInt(e.currentTarget.name); i < database.portfolio.length - 1; i++){
                    database.portfolio[i] = database.portfolio[i + 1];
                }
                database.portfolio.pop();
            }
        
            var backPage = false;
            if(pageCounter == totalPages){
                if(totalPages > 1) pageCounter--;
                backPage = true;
            }
        
            var listContainer = document.getElementById('portfolioList');
            listContainer.innerHTML = "";
            var portfolioList = document.createElement('ul');
            portfolioList.id = "portPage1";
            portfolioList.style.display = "none";
            listContainer.appendChild(portfolioList);
            var _pageCounter = 1;
            for(i = 0; i < database.portfolio.length; i++){
                if(i / _pageCounter == 10){
                    _pageCounter++;
                    portfolioList = document.createElement('ul');
                    portfolioList.id = "portPage" + _pageCounter;
                    portfolioList.style.display = "none";
                    document.getElementById('portfolioList').appendChild(portfolioList);
                }
                var portItem = document.createElement('li');
                portItem.className = "portfolioLink";
                portItem.innerHTML = '<a title="Remover" name="' + i + '" href="#delPortfolio' + (i + 1) + '" onClick="removePortfolio(event)"><img style="margin:5px 15px 0px 0px;" src="img/delete.png"></a><a name="' + i +' " href="#portfolio' + (i + 1) + '" onClick="changePortfolio(event)">&Iacute;tem ' + (i + 1) + '</a>';
                portfolioList.appendChild(portItem);
            }
        
            totalPages = _pageCounter;
            if(backPage){
                changePortPage(totalPages, true);
            } else {
                changePortPage(pageCounter, true);
            }
        
            if(parseInt(e.currentTarget.name) <= current){
                switchItem(current - 1);
            } else{
                switchItem(current);
            }
        
        } else{
            alert("Tem que existir no mínimo 1 ítem de portfolio.");
        }
    }
}

function newPortfolio(){
    overlayLightBox();
}

function updatePortfolioData(e){
    switch(e.target.id){
        case 'portName':
            database.portfolio[current].name = e.target.value;
            break;
        case 'portDesc':
            database.portfolio[current].desc = e.target.value;
            break;
        case 'portTech':
            database.portfolio[current].tech = e.target.value;
            break;
        case 'portURL':
            database.portfolio[current].link = e.target.value;
            break;
        case 'portStatus':
            database.portfolio[current].status = e.target.value;
            break;
    }
}
////////PORTFOLIO////////

////////ABOUT ME////////
function updateAboutMeData(e){
    if(e.currentTarget.name == 'aboutMeDesc'){
        database.aboutMe.desc = e.currentTarget.value;
    } else{
        var targetId = e.currentTarget.id;
        targetId = parseInt(targetId.substr(2, 1));
        
        if(e.currentTarget.name == 'skillName'){
            database.aboutMe.skills[targetId].name = e.currentTarget.value;
        } else {
            if(parseInt(e.currentTarget.value) > 100){
                e.currentTarget.value = 100;
            } else if(parseInt(e.currentTarget.value) < 1){
                e.currentTarget.value = 1;
            }
            database.aboutMe.skills[targetId].level = e.currentTarget.value;
        }
    }
}

function newSkill(){
    if(database.aboutMe.skills.length < 13){
        var obj = new Object();
        obj.name = "";
        obj.level = "";
        database.aboutMe.skills.push(obj);
        
        var skillRow = document.createElement('tr');
        var remSkill = document.createElement('td');
        remSkill.innerHTML = '<a title="Remover" href="#delSkill' + database.aboutMe.skills.length + '" name="' + (database.aboutMe.skills.length - 1) + '" onClick="removeSkill(event)"><img src="img/delete.png"></a>';
        var skillName = document.createElement('td');
        skillName.innerHTML = '<input id="sn' + (database.aboutMe.skills.length - 1) + '" name="skillName" style="width:100%;" type="text" value="" onChange="updateAboutMeData(event)"/>';
        var skillLevel = document.createElement('td');
        skillLevel.innerHTML = '<input id="sl' + (database.aboutMe.skills.length - 1) + '" name="skillLevel" style="width:75px;" type="text" maxlength="3" value="" onkeypress="onlyNumbers(event)" onChange="updateAboutMeData(event)"/>';
        
        skillRow.appendChild(remSkill);
        skillRow.appendChild(skillName);
        skillRow.appendChild(skillLevel);
        
        document.getElementById('skillsTable').appendChild(skillRow);
    } else{
        alert("Você não poder adicionar mais do que 13 skills na lista.");
    }
}

function removeSkill(e){
    var delConfirm = confirm("Deseja remover \"" + database.aboutMe.skills[parseInt(e.currentTarget.name)].name + "\" da lista de skills?");
    
    if(delConfirm){
        if(database.aboutMe.skills.length > 1){
            if(parseInt(e.currentTarget.name) == database.aboutMe.skills.length - 1){
                database.aboutMe.skills.pop();
            } else {
                for(var i = parseInt(e.currentTarget.name); i < database.aboutMe.skills.length - 1; i++){
                    database.aboutMe.skills[i] = database.aboutMe.skills[i + 1];
                }
                database.aboutMe.skills.pop();
            }
        
            var skillsTable = document.getElementById('skillsTable');
            skillsTable.innerHTML = "";
            for(i = 0; i < database.aboutMe.skills.length; i++){
                var skillRow = document.createElement('tr');
                var remSkill = document.createElement('td');
                remSkill.innerHTML = '<a title="Remover" href="#delSkill' + (i + 1) + '" name="' + i + '" onClick="removeSkill(event)"><img src="img/delete.png"></a>';
                var skillName = document.createElement('td');
                skillName.innerHTML = '<input id="sn' + i + '" name="skillName" style="width:100%;" type="text" value="' + database.aboutMe.skills[i].name + '" onChange="updateAboutMeData(event)"/>';
                var skillLevel = document.createElement('td');
                skillLevel.innerHTML = '<input id="sl' + i + '" name="skillLevel" style="width:75px;" type="text" maxlength="3" value="' + database.aboutMe.skills[i].level + '" onkeypress="onlyNumbers(event)" onChange="updateAboutMeData(event)"/>';
        
                skillRow.appendChild(remSkill);
                skillRow.appendChild(skillName);
                skillRow.appendChild(skillLevel);
        
                document.getElementById('skillsTable').appendChild(skillRow);
            }
        } else{
            alert("Você não pode ter menos de 1 skill na lista.");
        }
    }
}

function onlyNumbers(e){
    var key = e.keyCode;
    key = String.fromCharCode(key);
    var regex = /[0-9]/;
    
    if(!regex.test(key)){
        e.preventDefault();
    }
}
////////ABOUT ME////////